package com.ctg.behavior.calc.impala.transfer.funnel;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.ctg.behavior.calc.common.CtSQLConsts;
import com.ctg.behavior.calc.common.CtSQLParam;
import com.ctg.behavior.calc.impala.report.funnel.CtCalcFunnelStepReport;
import com.ctg.behavior.calc.impala.transfer.CtFilterTransfer;
import com.ctg.behavior.calc.utils.CtCalcPropertyUtils;
import com.ctg.behavior.common.calc.CtCalc;
import com.ctg.behavior.common.calc.CtCalcFilter;
import com.ctg.behavior.common.calc.CtCalcPropertyType;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtFunnelStepTransfer extends CtFilterTransfer {

	/******************************************************
	 * @apiNote: calcObject中包含漏斗步骤计算所需要的所有数据：步骤条件、全局条件、分组信息
	 */
	@Override
	public String doTransfer(CtCalc calcObject, CtSQLParam sqlParam) {

		if (!(calcObject instanceof CtCalcFunnelStepReport)) {
			log.warn("calcObject is not CtCalcFunnelStepReport, class={}", calcObject.getClass());
			return "";
		}
		CtCalcFunnelStepReport funnelStep = (CtCalcFunnelStepReport) calcObject;

		StringBuilder impalaSQL = new StringBuilder();
		
		//0. 使用用户维度
		{
			// 使用用户维度统计
			impalaSQL.append("SELECT T1.event AS event, T1.eventDate AS eventDate ");
			// byFields
			List<String> byFields = funnelStep.getByFields();
			for (String oneField : byFields) {
				CtCalcPropertyType valueType = CtCalcPropertyUtils.getValueType(oneField);
				impalaSQL.append(" , T1.").append(valueType.getProperty()).append(" AS ").append(valueType.getProperty());
			}
			impalaSQL.append(" , count(*) AS total FROM ( ");
		}
		// 1. SELECT 部分：日期(天:YYYY-MM-DD), byFields中字段, COUNT(*) total
		{
			impalaSQL.append("SELECT ");
			// event_date
			impalaSQL.append("event AS event, event_date AS eventDate, user_id AS userId ");
			// byFields
			List<String> byFields = funnelStep.getByFields();
			for (String oneField : byFields) {
				CtCalcPropertyType valueType = CtCalcPropertyUtils.getValueType(oneField);
				impalaSQL.append(" , ").append(valueType.getProperty()).append(" AS ").append(valueType.getProperty());
			}
			// 结果值(total)
			impalaSQL.append(" , COUNT(*) AS total ");
		}
		// 2. 表关联
		{
			// 根据APP_ID来决定查询哪个表
			String appId = funnelStep.getAppId();
			impalaSQL.append(" FROM a_event_parquet_").append(appId).append(" WHERE 1=1 ");
		}
		// 3. 查询条件 - 步骤中过滤条件
		{
			CtCalcFilter stepFilter = funnelStep.getFilter();
			String sqlStepFilter = super.doTransfer(stepFilter, sqlParam);
			log.info("sqlStepFilter={}", sqlStepFilter);
			if (!StringUtils.isEmpty(sqlStepFilter)) {
				impalaSQL.append(" AND ").append(sqlStepFilter);
			}
		}
		// 4. 查询条件 - 全局过滤条件
		{
			CtCalcFilter globalFilter = funnelStep.getGlobalFilter();
			String sqlGlobalFilter = super.doTransfer(globalFilter, sqlParam);
			log.info("sqlGlobalFilter={}", sqlGlobalFilter);
			if (!StringUtils.isEmpty(sqlGlobalFilter)) {
				impalaSQL.append(" AND ").append(sqlGlobalFilter);
			}
		}
		// 5.事件查询。
		{
			String eventId = funnelStep.getEventId();
			impalaSQL.append(" AND ").append("event='").append(eventId).append("'");
		}
		// 6.时间段
		{
			String fromDate = funnelStep.getFromDate();
			if (StringUtils.isNotBlank(sqlParam.getParam(CtSQLConsts.KEY_FILTER_DATE_FROM))) {
				fromDate = sqlParam.getParam(CtSQLConsts.KEY_FILTER_DATE_FROM);
			}
			if (StringUtils.isNotBlank(fromDate)) {
				impalaSQL.append(" AND ").append("event_date >='").append(fromDate).append("'");
			}
			
			String toDate = funnelStep.getToDate();
			if (StringUtils.isNotBlank(sqlParam.getParam(CtSQLConsts.KEY_FILTER_DATE_TO))) {
				toDate = sqlParam.getParam(CtSQLConsts.KEY_FILTER_DATE_TO);
			}
			if (StringUtils.isNotBlank(toDate)) {
				impalaSQL.append(" AND ").append("event_date <='").append(toDate).append("'");
			}
		}
		
		// ...
		{
			String userIdsSQL = sqlParam.getParam(CtSQLConsts.KEY_FILTER_SQL);
			if (!StringUtils.isEmpty(userIdsSQL)) {
				impalaSQL.append(" AND ").append("user_id IN ( ");
				impalaSQL.append(userIdsSQL);
				impalaSQL.append(" ) ");
			}
		}
		
		// 7. 分组(GROUP BY) - 若是整体，则是对日期分组统计
		{
			impalaSQL.append(" GROUP BY event, event_date, user_id ");
			// byFields
			List<String> byFields = funnelStep.getByFields();
			for (String oneField : byFields) {
				CtCalcPropertyType valueType = CtCalcPropertyUtils.getValueType(oneField);
				impalaSQL.append(" , ").append(valueType.getProperty());
			}
			// impalaSQL.append(" ORDER BY event_date ");
		}
		//8. 用户维度
		{
			impalaSQL.append(" ) T1 ");
			impalaSQL.append(" GROUP BY T1.event, T1.eventDate ");
			// byFields
			List<String> byFields = funnelStep.getByFields();
			for (String oneField : byFields) {
				CtCalcPropertyType valueType = CtCalcPropertyUtils.getValueType(oneField);
				impalaSQL.append(" , T1.").append(valueType.getProperty());
			}
		}
		
		return impalaSQL.toString();

	}

}
